// Package service @author dingrui @since 2021/11/26
package service

import (
	"github.com/bannirui/mini-prize/dal/dao"
	"github.com/bannirui/mini-prize/dal/model"
)

var (
	// 单例
	G_couponService CouponService
)

type CouponService interface {
	GetAll(page, size int) []model.Coupon
	CountAll() int64
	CountByGift(giftId int) int64
	Search(giftId int) []model.Coupon
	Get(id int) *model.Coupon
	Delete(id int) error
	Update(data *model.Coupon, columns []string) error
	Create(data *model.Coupon) error
	NextUsingCode(giftId, codeId int) *model.Coupon
	UpdateByCode(data *model.Coupon, columns []string) error
}

type couponService struct {
	dao *dao.CouponDao
}

func init() {
	G_couponService = &couponService{dao: dao.G_couponDao}
}

func (s *couponService) GetAll(page, size int) []model.Coupon {
	return s.dao.PageAll(page, size)
}

func (s *couponService) CountAll() int64 {
	return s.dao.CountAll()
}

func (s *couponService) CountByGift(giftId int) int64 {
	return s.dao.CountByGift(giftId)
}

func (s *couponService) Search(giftId int) []model.Coupon {
	return s.dao.ListByGiftId(giftId)
}

func (s *couponService) Get(id int) *model.Coupon {
	return s.dao.GetById(id)
}

func (s *couponService) Delete(id int) error {
	return s.dao.DeleteById(id)
}

func (s *couponService) Update(data *model.Coupon, columns []string) error {
	return s.dao.Update(data, columns)
}

func (s *couponService) Create(data *model.Coupon) error {
	return s.dao.InsertOne(data)
}

func (s *couponService) NextUsingCode(giftId, codeId int) *model.Coupon {
	return s.dao.GetAvailableCoupon(giftId, codeId)
}

func (s *couponService) UpdateByCode(data *model.Coupon, columns []string) error {
	return s.dao.UpdateByCode(data, columns)
}
